/*
 * AntBuildFileAspect.java
 *
 * Copyright (c) 1999-2011 Star-Storage. All rights reserved.
 *
 * This software is the confidential and proprietary information of Star Storage. 
 * You shall not disclose such Confidential Information and shall use it only in 
 * accordance with the terms of the license agreement you entered into with Star-Storage.
 *	
 * Version Info:
 *
 */

package ro.ss.bb.deployer.antAspect;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import ro.ss.bb.deployer.invoker.AntInvoker;
import ro.ss.bb.deployer.ui.start.DeployerUI;

/**
 * Replace with your class description.
 */
@Aspect
public class AntBuildFileAspect {

    private String[] getCfgFiles() {
        AntInvoker ai = new AntInvoker();
        String confFiles = ai.getP().getProperty(AntInvoker.CONFIG_FILES_PROPERTIES_TAG);
        String[] files = confFiles.trim().split(";");
        return files;
    }

    @Before("call (* ro.ss.bb.deployer.ui.start.DeployerUI.initPage(..)) && args(index)")
    public void readConfigFiles(JoinPoint joinPoint, int index) {
        String[] files = getCfgFiles();
        ((DeployerUI) joinPoint.getTarget()).numberOfPages = files.length;
        String content = "";
        try {
            content = readFile(files[index].trim());
        } catch (FileNotFoundException e) {
            System.err.println("FILE NOT FOUNT!");
        } catch (IOException e) {
            System.err.println("IO ERROR!");
        }
        ((ro.ss.bb.deployer.ui.start.DeployerUI) joinPoint.getTarget()).fileContent = content;
    }

    @After("call (* ro.ss.bb.deployer.ui.start.DeployerUI.clearPage(..)) && args(index)")
    public void saveConfigFiles(JoinPoint joinPoint, int index) {
        String[] files = getCfgFiles();
        try {
            saveFile(files[index].trim(), ((DeployerUI) joinPoint.getTarget()).fileContent);
        } catch (FileNotFoundException e) {
            System.err.println("FILE NOT FOUNT!");
        } catch (IOException e) {
            System.err.println("IO ERROR!");
        }
    }

    private String readFile(String path) throws IOException, FileNotFoundException {
        File f = new File(path);
        FileInputStream fstream = new FileInputStream(f);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        // Read File Line By Line

        StringBuilder content = new StringBuilder("");
        while ((strLine = br.readLine()) != null) {
            content.append(strLine).append("\n");
        }
        // Close the input stream
        br.close();
        in.close();
        return content.toString();
    }

    private void saveFile(String path, String content) throws FileNotFoundException, IOException {
        File g = new File(path);
        FileOutputStream gs = new FileOutputStream(g);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(gs));
        bw.write(content);
        bw.flush();
        bw.close();
        gs.close();
    }
}
